﻿using CatUtils.Utils.Currency;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CatUtils.Utils.SpecialUtils.View
{
    /// <summary>
    /// 异步更新界面内容
    /// </summary>
    public class ViewAsyncUpdate
    {
        /// <summary>
        /// 判断控件是否被创建，被创建以后才能进行操作
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        private static async Task<bool> HasCreated(Control control)
        {
            return await Task.Run(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    if (control.Created)
                    {
                        return true;
                    }
                    if (i > 1)
                        PrintLog.V($"控件({control.Name})未创建,等待{100 * i}毫秒。");
                    Thread.Sleep(100);
                }
                PrintLog.E($"当前控件({control.Name})未创建，在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。请检查程序调用顺序。");
                return false;
            }
                     );
        }

        /// <summary>
        /// 更新控件文本通用方法
        /// </summary>
        /// <param name="control"></param>
        /// <param name="TipText"></param>
        private async Task UpdateControlText(Control control, string TipText)
        {
            try
            {
                if (await HasCreated(control))
                {
                    Action act = () => control.Text = TipText; control.Invoke(act);
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 更新控件文本通用方法，配置并且保存相关数据
        /// </summary>
        /// <param name="TipLable"></param>
        /// <param name="TipText"></param>
        /// <param name="MemoryControlName"></param>
        /// <returns></returns>
        public async Task LableUpdateTextAsync(Label TipLable, string TipText, string MemoryControlName)
        {
            await LableUpdateTextAsync(TipLable, TipText);
            new ViewInfoMemory().SaveTextboxInfo(MemoryControlName, TipText);
        }

        /// <summary>
        /// 更新提示文本
        /// </summary>
        /// <param name="TipLable"></param>
        /// <param name="TipText"></param>
        public async Task LableUpdateTextAsync(Label TipLable, string TipText)
        {
            await UpdateControlText(TipLable, TipText);
        }

        /// <summary>
        /// groupbox异步清理界面
        /// </summary>
        /// <param name="groupBox"></param>
        public async Task GroupBoxClear(GroupBox groupBox)
        {
            try
            {
                if (await HasCreated(groupBox))
                {
                    Action act = () => groupBox.Controls.Clear();
                    groupBox.Invoke(act);
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 更新提示文本s
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="TipText"></param>
        /// <param name="MemoryControlName">保存这个控件变更信息，指定控件名字</param>
        /// <returns></returns>
        public async Task ComboBoxUpdateText(ComboBox comboBox, string TipText, string MemoryControlName)
        {
            await UpdateControlText(comboBox, TipText);

            new ViewInfoMemory().SaveComboBoxInfo(MemoryControlName, TipText);
        }

        /// <summary>
        /// 更新提示文本
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="TipText"></param>
        public async Task ComboBoxUpdateText(ComboBox comboBox, string TipText)
        {
            await UpdateControlText(comboBox, TipText);
        }

        /// <summary>
        /// 更新ComboBox文本
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="TipText"></param>
        public async Task ComboBoxAddItem(ComboBox comboBox, string TipText)
        {
            try
            {
                if (await HasCreated(comboBox))
                {
                    Action act = () => comboBox.Items.Add(TipText);
                    comboBox.Invoke(act);
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 设置选择ComboBox是否展开，展开为True
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="DroppedFlag">展开为True，默认为false</param>
        public async Task ComboBoxDropped(ComboBox comboBox, bool DroppedFlag)
        {
            try
            {
                if (await HasCreated(comboBox))
                {
                    Action act = () => comboBox.DroppedDown = DroppedFlag;
                    comboBox.Invoke(act);
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 清理ComboBox列表
        /// </summary>
        /// <param name="comboBox"></param>
        public async Task ComboBoxClearItem(ComboBox comboBox)
        {
            try
            {
                if (await HasCreated(comboBox))
                {
                    Action act = () => comboBox.Items.Clear();
                    comboBox.Invoke(act);
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 更新ComboBox文本
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="TipTextList"></param>
        public async Task ComboBoxAddItemList(ComboBox comboBox, List<string> TipTextList)
        {
            try
            {
                if (await HasCreated(comboBox))
                {
                    Action act = () => comboBox.Items.AddRange(TipTextList.ToArray());
                    comboBox.Invoke(act);
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// ComboBoxbox 更改选择index
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="SelecIndex"></param>
        /// <returns></returns>
        public async Task ComboBoxSelectedIndex(ComboBox comboBox, int SelecIndex)
        {
            try
            {
                if (await HasCreated(comboBox))
                {
                    Action act = () => comboBox.SelectedIndex = SelecIndex;
                    comboBox.Invoke(act);
                }
            }
            catch (Exception ex)
            {
                PrintLog.E(ex);
            }
        }

        /// <summary>
        /// 更新CheckBox选中状态
        /// </summary>
        /// <param name="checkBox"></param>
        /// <param name="CheckFalg"></param>
        /// <param name="MemoryControlName">保存这个控件变更信息，指定控件名字</param>
        /// <returns></returns>
        public async Task CheckBoxChecked(CheckBox checkBox, bool CheckFalg, string MemoryControlName)
        {
            await CheckBoxChecked(checkBox, CheckFalg);
            new ViewInfoMemory().SaveCheckboxInfo(MemoryControlName, CheckFalg.ToString());
        }

        /// <summary>
        /// 更新CheckBox选中状态
        /// </summary>
        /// <param name="checkBox"></param>
        /// <param name="CheckFalg"></param>
        /// <returns></returns>
        public async Task CheckBoxChecked(CheckBox checkBox, bool CheckFalg)
        {
            try
            {
                if (await HasCreated(checkBox))
                {
                    Action act = () => checkBox.Checked = CheckFalg;
                    checkBox.Invoke(act);
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        ///  groupbox异步添加新界面
        /// </summary>
        /// <param name="groupBox"></param>
        /// <param name="userControl"></param>
        public async Task GroupBoxAdd(GroupBox groupBox, UserControl userControl)
        {
            try
            {
                if (await HasCreated(groupBox))
                {
                    Action act = () => groupBox.Controls.Add(userControl);
                    groupBox.Invoke(act);
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 更新文本控件
        /// </summary>
        /// <param name="textBox"></param>
        /// <param name="Text"></param>
        /// <returns></returns>
        public async Task TextBoxUpdateText(TextBox textBox, string Text)
        {
            await UpdateControlText(textBox, Text);
        }

        /// <summary>
        /// 更新文本控件
        /// </summary>
        /// <param name="textBox"></param>
        /// <param name="Text"></param>
        /// <param name="MemoryControlName">保存这个控件变更信息，指定控件名字</param>
        /// <returns></returns>
        public async Task TextBoxUpdateText(TextBox textBox, string Text, string MemoryControlName)
        {
            await UpdateControlText(textBox, Text);
            new ViewInfoMemory().SaveTextboxInfo(MemoryControlName, Text);
        }

        /// <summary>
        /// 更新文本控件
        /// </summary>
        /// <param name="textBox"></param>
        /// <param name="Text"></param>
        /// <param name="MemoryControlName">保存这个控件变更信息，指定控件名字</param>
        /// <returns></returns>
        public async Task RichTextBoxUpdateText(RichTextBox textBox, string Text, string MemoryControlName)
        {
            await UpdateControlText(textBox, Text);
            new ViewInfoMemory().SaveRichTextboxInfo(MemoryControlName, Text);
        }

        /// <summary>
        /// 更新文本控件
        /// </summary>
        /// <param name="textBox"></param>
        /// <param name="Text"></param>
        public async Task RichTextBoxUpdateText(RichTextBox textBox, string Text)
        {
            await UpdateControlText(textBox, Text);
        }

        /// <summary>
        /// 更新Procress最大值
        /// </summary>
        /// <param name="RunProgress"></param>
        /// <param name="MaxValue"></param>
        public async Task ProcressSetMaxValue(ProgressBar RunProgress, int MaxValue)
        {
            try
            {
                if (await HasCreated(RunProgress))
                {
                    Action act = () => RunProgress.Maximum = MaxValue;
                    RunProgress.Invoke(act);
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 更新Procress最小值
        /// </summary>
        /// <param name="RunProgress"></param>
        /// <param name="MinValue"></param>
        public async Task ProcressSetMinValue(ProgressBar RunProgress, int MinValue)
        {
            try
            {
                if (await HasCreated(RunProgress))
                {
                    Action act = () => RunProgress.Minimum = MinValue;
                    RunProgress.Invoke(act);
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        ///更新Procress值
        /// </summary>
        /// <param name="RunProgress"></param>
        /// <param name="RunValue"></param>
        public async Task ProcressUpdateValue(ProgressBar RunProgress, int RunValue)
        {
            try
            {
                if (await HasCreated(RunProgress))
                {
                    Action act = () => RunProgress.Value = RunValue;
                    RunProgress.Invoke(act);
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }
    }
}